package com.helloandroid.ads

import android.app.Activity
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.anythink.rewardvideo.api.ATRewardVideoAd
import com.anythink.rewardvideo.api.ATRewardVideoListener
import com.business.TDKeys
import com.helloandroid.AppUtil
import com.helloandroid.MyApplication
import com.helloandroid.tools.AppConfig
import com.helloandroid.tools.MyLog
import com.helloandroid.vo.Type

object VideoAdUtil {

    // 是否正在播放
    var playing = false
//    var awardSuccess = false

    private val videoAdListener = object :ATRewardVideoListener{
        override fun onRewardedVideoAdLoaded() {
            MyLog.elog("激励广告缓存成功!!");
        }

        override fun onRewardedVideoAdFailed(adError: AdError?) {
            MyLog.elog("onRewardedVideoAdFailed:${adError?.fullErrorInfo}");
        }

        override fun onRewardedVideoAdPlayStart(p0: ATAdInfo?) {
            //awardSuccess = false
        }

        override fun onRewardedVideoAdPlayEnd(p0: ATAdInfo?) {
        }

        override fun onRewardedVideoAdPlayFailed(p0: AdError?, p1: ATAdInfo?) {
        }

        override fun onRewardedVideoAdClosed(p0: ATAdInfo?) {
            MyLog.info("===========[onRewardedVideoAdClosed called!")
//            if (awardSuccess){
//                videoCallback?.invoke(true)
//            }
            videoAd?.load()
            playing = false
        }

        override fun onRewardedVideoAdPlayClicked(p0: ATAdInfo?) {
        }

        override fun onReward(adInfo: ATAdInfo?) {
            MyLog.info("===========[onReward called!")
            AppUtil.appViewModel().zhuanReportTask(Type.ViewVideo)
//            awardSuccess = true
            videoCallback?.invoke(true)
        }
    }
    var videoAd:ATRewardVideoAd? = null
    var videoCallback :( (Boolean)->Unit)? = null

    fun initVideoAd(){
        if (videoAd == null){
            videoAd = ATRewardVideoAd(MyApplication.app.applicationContext,TDKeys.TopOnVideoPlacementId)
            videoAd!!.setAdListener(videoAdListener)
            videoAd!!.load()
        }
    }

    @JvmStatic
    fun playVideoAd(activity:Activity,callback:(Boolean)->Unit){
        MyLog.elog("=== playVideoAd called!")
        if (AppConfig.AdToggle && !AppConfig.isFake){
            videoCallback = callback
            if (videoAd!!.isAdReady){
                videoAd!!.show(activity)
                playing = true
            }else{
                videoAd!!.load()
                videoCallback?.invoke(false)
            }
        }else{
            callback.invoke(true)
        }
    }

//    @JvmStatic
//    fun playVideoInJava(activity: Activity,action: IVideoCallbackJava){
//        MyLog.elog("=== playVideoAd called!")
//        if (AppConfig.AdToggle && !AppConfig.isFake){
//            videoCallback = action::callback
//            if (videoAd!!.isAdReady){
//                videoAd!!.show(activity)
//                playing = true
//            }else{
//                videoAd!!.load()
//                videoCallback?.invoke(false)
//            }
//        }else{
//            action::callback.invoke(true)
//        }
//    }
}

//interface IVideoCallbackJava{
//    fun callback(success: Boolean)
//}